package com.ff.xyh.service.post.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ff.xyh.common.entity.post.PostJoiner;
import com.ff.xyh.common.entity.result.ResultObject;
import com.ff.xyh.common.qo.post.PostJoinerQuery;
import com.ff.xyh.service.post.mapper.PostJoinerMapper;
import com.ff.xyh.service.post.service.PostJoinerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 参与用户表 服务实现类
 * </p>
 *
 * @author ff
 * @since 2022-10-26
 */
@Service
public class PostJoinerServiceImpl extends ServiceImpl<PostJoinerMapper, PostJoiner> implements PostJoinerService {

    @Override
    public ResultObject getPostJoinerCondition(PostJoinerQuery postJoinerQuery) {

        Page<PostJoiner> page = new Page<>(postJoinerQuery.getCurrentPage(), postJoinerQuery.getPageSize());
//        List<PostJoiner> resultList = new ArrayList<>();
        QueryWrapper<PostJoiner> wrapper = new QueryWrapper<>();


        if (postJoinerQuery.getGmtAfter() != null) {
            wrapper.ge("gmt_created", postJoinerQuery.getGmtAfter());
        }
        if (postJoinerQuery.getGmtBefore() != null) {
            wrapper.le("gmt_created", postJoinerQuery.getGmtBefore());
        }
        // 按创建时间排序意味着要最新
        if (postJoinerQuery.getOrderByCreatedTime() != null && postJoinerQuery.getOrderByCreatedTime()) {
            // 降序
            wrapper.orderByDesc("gmt_created");
        } else {
            wrapper.orderByAsc("gmt_created");
        }

        if (!StringUtils.isEmpty(postJoinerQuery.getUserId())) {
            wrapper.eq("user_id", postJoinerQuery.getUserId());
        }
        if (!StringUtils.isEmpty(postJoinerQuery.getPostId())) {
            wrapper.eq("post_id", postJoinerQuery.getPostId());
        }
        if (postJoinerQuery.getPostType() != null) {
            wrapper.eq("post_type", postJoinerQuery.getPostType());
        }
        if (postJoinerQuery.getJoinerStatus() != null) {
            wrapper.eq("joiner_status", postJoinerQuery.getJoinerStatus());
        }


        IPage<PostJoiner> resultPage = this.page(page, wrapper);

        return ResultObject.ok().putList(resultPage.getRecords(), resultPage.getTotal());
    }
}
